clear all
local socsec $socsec
***SPOUSAL DI INFORMATION, 1986-2009 excluding 94-07
#delimit ;
*  PSID DATA CENTER *****************************************************
   JOBID            : 263915                            
   DATA_DOMAIN      : IND                               
   USER_WHERE       : NULL                              
   FILE_TYPE        : All Individuals Data              
   OUTPUT_DATA_TYPE : ASCII                             
   STATEMENTS       : do                                
   CODEBOOK_TYPE    : PDF                               
   N_OF_VARIABLES   : 23                                
   N_OF_OBSERVATIONS: 34339                             
   MAX_REC_LENGTH   : 58                                
   DATE & TIME      : September 4, 2019 @ 23:00:47
*************************************************************************
;

infix
      ER30000              1 - 1           ER30001              2 - 5           ER30002              6 - 8     
      ER34001              9 - 13          ER34002             14 - 15          ER34003             16 - 17    
      ER34030             18 - 18          ER34101             19 - 23          ER34102             24 - 25    
      ER34103             26 - 27          ER34137             28 - 28          ER34201             29 - 33    
      ER34202             34 - 35          ER34203             36 - 37          ER34244             38 - 38    
      ER34301             39 - 43          ER34302             44 - 45          ER34303             46 - 47    
      ER34394             48 - 48          ER34501             49 - 53          ER34502             54 - 55    
      ER34503             56 - 57          ER34603             58 - 58    
using J263915.txt, clear 
;
label variable ER30000       "RELEASE NUMBER"                           ;
label variable ER30001       "1968 INTERVIEW NUMBER"                    ;
label variable ER30002       "PERSON NUMBER                         68" ;
label variable ER34001       "2009 INTERVIEW NUMBER"                    ;
label variable ER34002       "SEQUENCE NUMBER                       09" ;
label variable ER34003       "RELATION TO HEAD                      09" ;
label variable ER34030       "G33 TYPE SOC SEC RCD                  09" ;
label variable ER34101       "2011 INTERVIEW NUMBER"                    ;
label variable ER34102       "SEQUENCE NUMBER                       11" ;
label variable ER34103       "RELATION TO HEAD                      11" ;
label variable ER34137       "G33A WTR SOC SEC TYPE DISABILITY      11" ;
label variable ER34201       "2013 INTERVIEW NUMBER"                    ;
label variable ER34202       "SEQUENCE NUMBER                       13" ;
label variable ER34203       "RELATION TO HEAD                      13" ;
label variable ER34244       "G33A WTR SOC SEC TYPE DISABILITY      13" ;
label variable ER34301       "2015 INTERVIEW NUMBER"                    ;
label variable ER34302       "SEQUENCE NUMBER                       15" ;
label variable ER34303       "RELATION TO HEAD                      15" ;
label variable ER34394       "G33A WTR SOC SEC TYPE DISABILITY      15" ;
label variable ER34501       "2017 INTERVIEW NUMBER"                    ;
label variable ER34502       "SEQUENCE NUMBER                       17" ;
label variable ER34503       "RELATION TO REFERENCE PERSON          17" ;
label variable ER34603       "G33A WTR SOC SEC TYPE DISABILITY      17" ;

#delimit cr

gen pid9=ER34003==10 & ER34002>=1 & ER34002<=20
gen pid11=ER34103==10 & ER34102>=1 & ER34102<=20
gen pid13=ER34203==10 & ER34202>=1 & ER34202<=20
gen pid15=ER34303==10 & ER34302>=1 & ER34302<=20
gen pid17=ER34503==10 & ER34502>=1 & ER34502<=20

local DI ER34137 ER34244 ER34394  ER34603
local DI_years 11 13 15 17

local rel ER34003 ER34103 ER34203 ER34303 ER34503
local rel_years 9 11 13 15 17

local seq ER34002 ER34102 ER34202 ER34302 ER34502
local seq_years 9 11 13 15 17

rename ER34030 typen9

tokenize `seq_years'
foreach x of local seq{
rename `x' seq`1'
macro shift
}

tokenize `DI_years'
foreach x of local DI{
rename `x' DI`1'
macro shift
}

tokenize `rel_years'
foreach x of local rel{
rename `x' headrel`1'
macro shift
}
gen DI9 = typen9==1

*family IDs
ren ER34001 id9
ren ER34101 id11
ren ER34201 id13
ren ER34301 id15
ren ER34501 id17

drop ER*

gen person=1
replace person=sum(person)
reshape long pid seq id famid headrel typen DI, i(person) j(year)

replace year = year+1900 if year >= 50
replace year = year+2000 if year <50
replace year = year-1

*Keeping the spouses, matching them into the main file
gen spouse = headrel >=20 & headrel < 30 & seq==2
bysort year id: egen totspouses=sum(spouse)
*only one spouse per id-year! 
drop totspouses

keep if spouse==1
rename DI sp_DI
rename typen sp_typen
keep id year sp_DI  sp_typen
tempfile DI1
save `DI1', replace

**Completing spousal DI information
insheet using "`socsec'\socsectype94_07.csv", comma clear
rename yrid id
rename hdsocsectype typen
rename wfsocsectype sp_typen
gen sp_DI = sp_typen==1
replace year = year-1
append using `DI1'
merge 1:1 id year using $intermediary/transfer_data.dta, replace update
**OLD CODE NOTES:
*data missing for hispanic subsample (will be dropped), in 1993-2007
*data missing in 2008 onward where I'm merging in info only for spouses
gen sample_DIfile = _merge == 3
drop _merge
preserve

*renaming the 2008 data from this file, sinec it also appears in next:
save $intermediary/transfer_DI_data.dta, replace
restore
